#include <iostream>

#include "./myrpc.h"
#include "xlcomm/net/event_loop_thread.h"
#include "xlcomm/net/rpc/rpc_server.h"
using namespace xlcomm;

class MathService {
 public:
  void Add(const net::RpcParamReader& reqReader, net::RpcParam& resParam) {
    std::cout << "----Add---- \n";
    MathReq req;
    reqReader.getItemData(req);

    MathResp resp;
    resp.result = req.a + req.b;
    resParam << resp;
  }

  void Sub(const net::RpcParamReader& reqReader, net::RpcParam& resParam) {
    std::cout << "----Sub---- \n";
    MathReq req;
    reqReader.getItemData(req);

    MathResp resp;
    resp.result = req.a - req.b;
    resParam << resp;
  }
};

int main() {
  // Logger::SetLogLevel(Logger::kTrace);
  net::EventLoop loop;
  net::RpcServer server(&loop, net::InetAddress(40003));

  MathService math;
  server.RegisterRpc("MathService", "Add",
                     std::bind(&MathService::Add, &math, _1, _2));
  server.RegisterRpc("MathService", "Sub",
                     std::bind(&MathService::Sub, &math, _1, _2));

  server.Start();
  loop.Loop();
}